package com.WOWTradeNews.tablet;

import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.List;
import android.content.Intent;
import android.database.Cursor;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ListView;
import android.widget.TextView;

import com.WOWTradeNewsTest.R;
import com.WOWTradeNews.DataBase.News;
import com.WOWTradeNews.DataBase.NewsDataBase;
import com.WOWTradeNews.tablet.NewsListAdapter;

public class TradeInformationList extends Default {	
	
	private NewsDataBase database;
	private Cursor myCursor;
	private ListView myListView;
	private String newsId;
	
	private List<News> items;
	
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.market_research);
		
		myListView = (ListView) findViewById(R.id.list_view);	
		
		try{		
			database = new NewsDataBase(this);
			myCursor = database.selectNews("3");
			
			items = new ArrayList<News>();
			
			DateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
			Date today = Calendar.getInstance().getTime();
			
			while(myCursor.moveToNext()) {
				News news = new News();
				
				String startDate=myCursor.getString(myCursor.getColumnIndex("dtStartDate"));
				String endDate =myCursor.getString(myCursor.getColumnIndex("dtEndDate"));

				boolean isAdd=false;
				
				if (startDate != null && startDate.trim().length()>0 && endDate != null && endDate.trim().length()>0) {
					startDate=startDate.replaceAll("T", " ");
					Date sDate = df.parse(startDate);
					endDate=endDate.replaceAll("T", " ");
					Date eDate = df.parse(endDate);
					if (today.after(sDate) && today.before(eDate)) {
						isAdd=true;
					}
				}
				else if (startDate != null && startDate.trim().length()>0) {
					startDate=startDate.replaceAll("T", " ");
					Date sDate = df.parse(startDate);
					if (today.after(sDate)) {
						isAdd=true;
					}
				}
				else if (endDate != null && endDate.trim().length()>0) {
					endDate=endDate.replaceAll("T", " ");
					Date eDate = df.parse(endDate);
					if (today.before(eDate)) {
						isAdd=true;
					}
				}
				
				if(isAdd){
					news.setPK_ID(myCursor.getString(myCursor.getColumnIndex("_id")));
					news.setImageUrl(myCursor.getString(myCursor.getColumnIndex("vchImageUrl")));
					news.setTitle(myCursor.getString(myCursor.getColumnIndex("vchTitle")));
					news.setSummary(myCursor.getString(myCursor.getColumnIndex("vchSummary")));
					items.add(news);
				}
			}
	
			NewsListAdapter myAdapter = new NewsListAdapter(this,items,myListView,3);
			myListView.setAdapter(myAdapter);			
			myListView.setOnItemClickListener(new OnItemClickListener() {
	
				@Override
				public void onItemClick(AdapterView<?> arg0, View arg1,int arg2, long arg3) {
					newsId = ((TextView) arg1.findViewById(R.id.ItemID)).getText().toString();
					
					Intent intent = new Intent();
					intent.setClass(TradeInformationList.this, TradeNewsDetail.class);
	
					intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
					Bundle bundle = new Bundle();
					bundle.putString("newsID", newsId);
					bundle.putString("newsType", "2");
					
					intent.putExtras(bundle);
					startActivity(intent);
				}
			});		
		}
		catch(Exception e){
			Log.e("onCreate", e.toString());
		}
		finally{
			if(!myCursor.isClosed()) myCursor.close();
			database.close();
		}
	}	
}
